<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class Goods extends Model
{
    use HasFactory;
    protected $fillable = ['cate_id', 'name', 'price', 'price_agent_one', 'price_agent_two', 'detail', 'good_img', 'good_img_small'];
    protected $dateFormat = 'U';

    public function setPriceAttribute($value) {
        $this->attributes['price'] = $value * 100;
    }

    public function setPriceAgentOneAttribute($value) {
        $this->attributes['price_agent_one'] = $value * 100;
    }

    public function setPriceAgentTwoAttribute($value) {
        $this->attributes['price_agent_two'] = $value * 100;
    }

    public function getPriceAttribute($value) {
        return sprintf("%1\$.2f",$value / 100);
    }

    public function getPriceAgentOneAttribute($value) {
        return sprintf("%1\$.2f", $value / 100);
    }

    public function getPriceAgentTwoAttribute($value) {
        return sprintf("%1\$.2f", $value / 100);
    }

    public function getCateIdAttribute($value) {
        $cateList = Cache::get('good_category', function() {
           return GoodCate::all()->toArray();
        });
        return getCateName($value, $cateList);
    }

    public function goodCate() {
        return $this->belongsTo(GoodCate::class, 'cate_id');
    }
}
